/*
 Práctica 1 asignatura Desarrollo Aplicaciones Móviles

    -Informática-

Integrantes:  Esperanza González de la Mora & Félix López Garcinuño & José Miguel Bilbao Navas
 */

package capa_interfaz.splash;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Form;

public class CanvasSplash extends Canvas implements Runnable{
   Displayable startScreen;
   Display display;
   Form pantallainicial;
   Image ic,cero,veinte,cuarenta,sesenta,ochenta,cien;
   int cargado;
   Thread pausa;
   
   public CanvasSplash(Display display,Form pantallainicial) {
      this.display = display;
      this.pantallainicial= pantallainicial;
      try {
           ic = Image.createImage("capa_interfaz/icono/micartera_ico_carga.png");
           cero = Image.createImage("capa_interfaz/imagenescarga/ceroporciento.png");
           veinte = Image.createImage("capa_interfaz/imagenescarga/veinteporciento.png");
           cuarenta = Image.createImage("capa_interfaz/imagenescarga/cuarentaporciento.png");
           sesenta = Image.createImage("capa_interfaz/imagenescarga/sesentaporciento.png");
           ochenta = Image.createImage("capa_interfaz/imagenescarga/ochentaporciento.png");
           cien = Image.createImage("capa_interfaz/imagenescarga/cienporciento.png");
          }
      catch (IOException e) {
           System.out.println("Error al cargar imágenes");
         }
   }
   public void start(){
       pausa=new Thread(this);
       pausa.start();
    }

   public void setStartScreen(Displayable startScreen) {
        this.startScreen = startScreen;
    }

    public void paint(Graphics g) {
        int divi=8;//Parámetros cambiarian según celular
        int cent=10;int alt1=120;int alt2=140;

        g.setColor(0,0,0); //Selecciono el color negro
        g.fillRect(0,0,getWidth(),getHeight()); //limpiamos pantalla
          if (cargado<=100){
              g.drawImage(ic,3*cent,getHeight()/divi , Graphics.TOP |Graphics.LEFT);//Muestro el icono de la aplicación
              g.setColor(255,255,255); //Selecciono el color blanco
          }
          if (cargado==0) {
            g.drawString("Carga      0%",getWidth()/divi+cent,getHeight()/divi+alt1,Graphics.TOP|Graphics.LEFT);
            g.drawImage(cero, getWidth()/divi, getHeight()/divi+alt2, Graphics.TOP |Graphics.LEFT);
          }
          if (cargado==20){
            g.drawString("Carga     20%",getWidth()/divi+cent,getHeight()/divi+alt1,Graphics.TOP|Graphics.LEFT);
            g.drawImage(veinte, getWidth()/divi, getHeight()/divi+alt2, Graphics.TOP |Graphics.LEFT);
          }
          if (cargado==40){
            g.drawString("Carga     40%",getWidth()/divi+cent,getHeight()/divi+alt1,Graphics.TOP|Graphics.LEFT);
            g.drawImage(cuarenta, getWidth()/divi, getHeight()/divi+alt2, Graphics.TOP |Graphics.LEFT);
          }
          if (cargado==60){
            g.drawString("Carga     60%",getWidth()/divi+cent,getHeight()/divi+alt1,Graphics.TOP|Graphics.LEFT);
            g.drawImage(sesenta, getWidth()/divi, getHeight()/divi+alt2, Graphics.TOP |Graphics.LEFT);
          }
          if (cargado==80){
            g.drawString("Carga     80%",getWidth()/divi+cent,getHeight()/divi+alt1,Graphics.TOP|Graphics.LEFT);
            g.drawImage(ochenta, getWidth()/divi, getHeight()/divi+alt2, Graphics.TOP |Graphics.LEFT);
          }
          if (cargado==100){
            g.drawString("Carga    100%",getWidth()/divi+cent,getHeight()/divi+alt1,Graphics.TOP|Graphics.LEFT);
            g.drawImage(cien, getWidth()/divi, getHeight()/divi+alt2, Graphics.TOP |Graphics.LEFT);
          }
    }

    public void run(){
      cargado=0;
        try{
           do{
              pausa.sleep(2000); cargado+=20; repaint();
           }while (cargado<100);
      pausa.sleep(2000);System.out.println("Pantalla principal iniciada");
      display.setCurrent(pantallainicial);//Establecemos la pantalla
       }
       catch (InterruptedException e) {
         System.out.println("Error al iniciar pantalla de carga de la aplicación");
       }
    }
}

